package project2.ui.manage;

import project2.config.Config;
import project2.util.SoundPlayer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;

/**
 * 因为要实现带图片和声音的按钮，才自定义按钮类
 */
public class GameButton extends JButton implements MouseListener {
    private static final long serialVersionUID = 1L;
    private final String BUTTON_HOVER = "BUTTON_HOVER";// 点击
    private final String BUTTON_NORMAL = "BUTTON_NORMAL";// 未点击

    private String text;// 按钮上的文字
    private String buttonStatus;// 按钮状态
    private SoundPlayer buttonSoundPlayer;// 按钮声音

    public GameButton() {
        super();
        this.text = "";
        initButton();
    }

    public GameButton(String text) {
        super();
        this.text = text;
        initButton();
    }

    /**
     * 初始化按钮
     */
    private void initButton() {
        // 状态初始化
        this.buttonStatus = BUTTON_NORMAL;
        // 触发行为全部关闭
        this.setBorderPainted(false);
        this.setFocusPainted(false);
        this.setContentAreaFilled(false);
        this.addMouseListener(this);
        this.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 创建声音
        try {
            this.buttonSoundPlayer = new SoundPlayer(Config.BUTTON_ACTION_AUDIO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 根据状态选择图片
        Image buttonBgImg;
        if (this.buttonStatus.equals(BUTTON_NORMAL)) {
            buttonBgImg = new ImageIcon(Config.BUTTON_BG_IMG).getImage();
        } else if (this.buttonStatus.equals(BUTTON_HOVER)) {
            buttonBgImg = new ImageIcon(Config.BUTTON_HOVER_BG_IMG).getImage();
        } else {
            return;
        }
        int buttonWidth = buttonBgImg.getWidth(this);
        int buttonHeight = buttonBgImg.getHeight(this);

        this.setSize(buttonWidth, buttonHeight);// 设置按钮宽高
        this.setPreferredSize(new Dimension(buttonWidth, buttonHeight));// 设置按钮宽高

        // 绘制按钮
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(buttonBgImg, 0, 0, buttonWidth, buttonHeight, this);// 绘制按钮图片
        FontMetrics metric = g.getFontMetrics();
        Rectangle2D rect = metric.getStringBounds(text, g);
        float text_w = (float) (buttonWidth / 2 - rect.getWidth() / 2);
        float text_h = (float) ((buttonHeight / 2) + ((metric.getAscent() + metric.getDescent()) / 2 - metric.getDescent()));
        g2d.drawString(text, text_w, text_h);// 绘制按钮文字
    }

    // 变换按钮状态

    private void buttonHover() {
        this.buttonStatus = BUTTON_HOVER;
    }

    private void buttonNormal() {
        this.buttonStatus = BUTTON_NORMAL;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        buttonHover();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        buttonHover();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        buttonNormal();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        buttonHover();
        this.buttonSoundPlayer.play();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        buttonNormal();
    }

}
